let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:0,//字数
    sub: false,
    order_id:'',
    goods_id:'',
    info:'',
    goods_spec_info:'',
    return_cash:'',
    nums:1,
    selectDrap: true,
    currentReason: '',
    return_reason_id:'',
    selectArea: false,

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.ToastPannel();
    var that = this
    var k = wx.getStorageSync('key')
    var u = wx.getStorageSync('u')
    var order_id = options.order_id
    var order_goods_id = options.order_goods_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=index&act=add&typ=json',
      method: 'GET',
      data: { k: k, u: u, oid: order_id, gid: order_goods_id },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var goods_spec_info = ''
        if (res.data.status == 200) {
          if (res.data.data.goods.order_spec_info && res.data.data.goods.order_spec_info.length > 0){
            var order_spec_info = res.data.data.goods.order_spec_info
            for (var i in order_spec_info){
              goods_spec_info += order_spec_info[i] + ';';
            }
          }
          that.setData({
            info:res.data.data,
            order_id: order_id,
            goods_id: order_goods_id,
            goods_spec_info: goods_spec_info,
            return_cash: res.data.data.return_cash,
            nums: res.data.data.return_goods_nums,
            currentReason: res.data.data.reason[0].order_return_reason_content,
            return_reason_id: res.data.data.reason[0].id
          })
        }
      }
    })
  },
  
  formSubmit: function (e) {
    var that = this
    var k = wx.getStorageSync('key')
    var u = wx.getStorageSync('u')
    var nums = e.detail.value.nums
    var return_message = e.detail.value.return_message
    if (!return_message){
      that.show('请填写退款说明')
      return false;
    }
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=addReturn&typ=json',
      method: 'GET',
      data: { k: k, u: u, order_id: that.data.order_id, goods_id: that.data.goods_id, return_cash: that.data.return_cash, return_message: return_message, nums: nums, return_reason_id: that.data.return_reason_id},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
          wx.redirectTo({
            url: '../member_refund/member_refund',
          })
        }
      }
    })
  },
  //退货说明
  return_message: function (e) {
    var that = this
    var cursor = e.detail.cursor
    if (cursor > 0) {
      that.setData({
        sub: true,
        num: cursor
      })
    } else {
      that.setData({
        sub: false,
        num: cursor
      })
    }

  },
  //减数量
  reduce:function(){
    var that = this
    if (that.data.nums == 1){
      return false
    }
    var nums = that.data.nums - 1
    that.setData({
      nums: nums,
      return_cash: nums * Number(that.data.info.goods.order_goods_payment_amount)
    })
  },
  //加
  add:function(){
    var that = this
    var nums = that.data.nums + 1
    that.setData({
      nums: nums,
      return_cash: nums * Number(that.data.info.goods.order_goods_payment_amount)
    })
  },
  // 退款原因
  //点击选择类型
  clickSelect: function () {
    var selectDrap = this.data.selectDrap;
    if (selectDrap == true) {
      this.setData({
        selectArea: true,
        selectDrap: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectDrap: true,
      })
    }
  },
  //点击切换
  mySelect: function (e) {
    console.log(e);
    this.setData({
      currentReason: e.target.dataset.me,
      return_reason_id: e.target.dataset.id,
      selectDrap: true,
      selectArea: false
    })
  },
  
})